package com.org.daniel.service;

import com.github.pagehelper.PageHelper;
import com.org.daniel.common.constant.enumerators.RedisChannelEnum;
import com.org.daniel.mapper.master.UserInfoMapper;
import com.org.daniel.mapper.second.UserInfoSecondMapper;
import com.org.daniel.module.domain.ResultResponse;
import com.org.daniel.module.entity.master.UserInfo;
import com.org.daniel.module.entity.second.UserInfoSecond;
import com.org.daniel.service.util.RedisManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Daniel
 */
@Service
public class UserInfoService {
	private static Logger logger = LoggerFactory.getLogger(UserInfoService.class);

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private UserInfoSecondMapper userInfoSecondMapper;

	@Autowired
	private RedisManagerService redisManagerService;

	public ResultResponse queryAllUserInfo(){
		List<UserInfo> list = userInfoMapper.queryAll();
		List<UserInfoSecond> listSecond = userInfoSecondMapper.queryAll();
		Map<String, Object> map = new HashMap<>();
		map.put("listMaster", list);
		map.put("listSecond", listSecond);
		ResultResponse resultResponse = new ResultResponse();
		resultResponse.setResultCode("1");
		resultResponse.setResultMessage("success");
		resultResponse.setData(map);
		return resultResponse;
	}

	public ResultResponse<List<UserInfo>> queryUserInfoByPage(int pageNum, int pageSize){
		PageHelper.startPage(pageNum, pageSize);
		List<UserInfo> list = userInfoMapper.queryAll();
		ResultResponse<List<UserInfo>> resultResponse = new ResultResponse<>();
		resultResponse.setResultCode("1");
		resultResponse.setResultMessage("success");
		resultResponse.setData(list);
		return resultResponse;
	}

	public ResultResponse testRedis(String key, String value){
		redisManagerService.setValue(RedisChannelEnum.MASTER.getCode(), key, value+"aaa", 10, TimeUnit.MINUTES);
		redisManagerService.setValue(RedisChannelEnum.SECOND.getCode(), key, value+"bbb", 10, TimeUnit.MINUTES);
		Object masterValue = redisManagerService.getValue(RedisChannelEnum.MASTER.getCode(), key);
		Object secondValue = redisManagerService.getValue(RedisChannelEnum.SECOND.getCode(), key);
		logger.info("masterValue={}, secondValue={}", masterValue, secondValue);
		ResultResponse response = new ResultResponse();
		response.setResultCode("1");
		response.setResultMessage("success");
		return response;
	}
}
